package de.uni.kassel.campusappver3;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import de.uni.kassel.campusappver3.model.Resources;
import de.uni.kassel.campusappver3.view.BahnActivity;
import de.uni.kassel.campusappver3.view.EventActivity;
import de.uni.kassel.campusappver3.view.ListActivity;
import de.uni.kassel.campusappver3.view.MapActivity;
import de.uni.kassel.campusappver3.view.subview.MensaActivity;

/**
 * Shows the main screen with its tabs
 * using TabActivity instead of Fragments to support lower API levels
 * @author Tohme
 *
 */
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
	
	public static boolean internetConnection;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        internetConnection = isOnline();
 
        TabHost tabHost = getTabHost();
        Resources.getInstance().setTabHost(tabHost);
        TabSpec mapspec = tabHost.newTabSpec("Map");
        mapspec.setIndicator("MAP");
        Intent mapIntent = new Intent(this, MapActivity.class);
        mapspec.setContent(mapIntent);
 
        TabSpec mensaspec = tabHost.newTabSpec("Mensa");
        mensaspec.setIndicator("MENSA");
        Intent mensaIntent = new Intent(this, MensaActivity.class);
        mensaspec.setContent(mensaIntent);
        
        TabSpec bahnspec = tabHost.newTabSpec("Bahn");
        bahnspec.setIndicator("BAHN");
        Intent bahnIntent = new Intent(this, BahnActivity.class);
        bahnspec.setContent(bahnIntent);
        
        TabSpec stundenspec = tabHost.newTabSpec("Events");
        stundenspec.setIndicator("EVENTS");
        Intent eventIntent = new Intent(this, EventActivity.class);
        stundenspec.setContent(eventIntent);
        
        TabSpec listspec = tabHost.newTabSpec("List");
        listspec.setIndicator("LIST");
        Intent listIntent = new Intent(this, ListActivity.class);
        listspec.setContent(listIntent);
 
        tabHost.addTab(mapspec);  
        tabHost.addTab(mensaspec); 
        tabHost.addTab(bahnspec); 
        tabHost.addTab(stundenspec);
        tabHost.addTab(listspec);
        
        Resources.setTabColor(getTabHost());
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				Resources.setTabColor(getTabHost());
			}
		});
    }
	
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	public boolean isOnline() {
	    ConnectivityManager cm =
	        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
	        return true;
	    }
	    return false;
	}

}
